app.factory("basicHttp", ["$http", function ($http) {
    var header;
    var apiUrl = "";
    if (!isLocalDebug) {
        header = {
            'ApiKey': getStorageValue('ApiKey'),
            'DeviceId': getStorageValue('DeviceId'),
            'AuthToken':null,
            'IsTokenEncrypted': false,
            'Content-Type': "application/json;charset=utf-8"
        };
        apiUrl = "http://" + getStorageValue('env') + "/";
    }
    else {
        header = {
            'ApiKey': "1cc26ed0fd2111e49cf72761d4344ff7",
            'DeviceId': "dongmingdeviceid1234",
            'AuthToken':null,
            'isTokenEncrypted': false,
            'Content-Type': "application/json;charset=utf-8"
        };
        /*apiUrl="http://cndr.lundao.cn/api/";*/
        /*apiUrl = "http://fs.lundao.cn/"*/
        apiUrl = "http://Api.fashionsingles.org/"
    }
   /* var apiVersion = "v1/";*/
    /*var prefix = apiUrl + apiVersion;*/
    var prefix = apiUrl;
    $http.defaults.headers.common = header;
    var basicHttp = {
        'http': $http,
        'prefixUrl': prefix,
        'commonHeader': header,
        'setAuthToken':function(){
            header.AuthToken=getAuthToken();
        }
    };
    return basicHttp;

}]);


app.factory("navigationService", function ($location, $routeParams, $route,globalVariableService,$rootScope){
    //console.log($location.path().split("/"))
    var getBackUrl = function(){
        var position = $location.path().split("/")[1] || '';
        var subPosition = $location.path().split("/")[2] || '';
        var thirdPosition = $location.path().split("/")[3] || '';
        switch (position){
                case "searchFriends":
                        return function(){
                            $location.path('/search');
                    };
                    break;

                case "myPersonInfo":
                    if(subPosition == 'myAbout'|| subPosition== 'integral' || subPosition=='myInfoDetails'){
                        return function(){
                            $location.path('myPersonInfo');
                        };
                    }
                    if(subPosition=='myInfo' && thirdPosition=='description'){
                        return function(){
                            $location.path('myInfoDetails/myInfo');
                        };
                    }
                    if(subPosition == 'pay'){
                        return function(){
                            $location.path('/');
                        }
                    }
                    if(subPosition == ""){
                        return function () {
                            if (window.WebViewJavascriptBridge) {
                                WebViewJavascriptBridge.callHandler('exitUIWebView');
                            } else if (browser.versions.android) {
                                try {
                                    jsb.require('exitUIWebView', { "msg": "navigate back" }, function (response) {
                                        //Log.log(response);
                                    });
                                }
                                catch (e) {
                                    console.log(e)
                                }
                            }
                        };
                    }
                    break;

                case "becomeVIP":
                    return function(){
                        $location.path('myPersonInfo');
                    };
                    break;

                case "playIntegral":
                    return function(){
                        $location.path('myPersonInfo/integral');
                    }
                    break;

                case "searchCountFull":
                    return function(){
                        $location.path('search');
                    }
                    break;

                case "myInfoDetails":
                    if(subPosition=='myInfo'){
                        if(globalVariableService.userInfo.user_status == 2){
                            return function(){
                                $location.path('myPersonInfo');
                            }
                        }else{
                            return function() {
                                $location.path('/');
                            }
                        }
                    };
                    break;

                case "user":
                    if(subPosition=='login'){
                        return function(){
                            $location.path('/');
                        }
                    };
                    break;

                case "integralPrompt":
                    return function(){
                        $location.path('search');
                    };
                    break;

                case "PayFails":
                    return function(){
                        $location.path('myPersonInfo/pay');
                    };
                    break;

                case "detailsInfo":
                    return function(){
                        $location.path('search');
                    };
                    break;
                case "PayFailsIntegral":
                    return function(){
                        $location.path('playIntegral');
                    };
                    break;

                case "collect":
                    return function(){
                        $location.path('myPersonInfo');
                    };
                    break;

                case "promptFeedback":
                    return function(){
                       $location.path('/');
                    };
                    break;

                case "SuccessPayIntegral":
                    return function(){
                        $location.path('/');
                    };
                    break;

                case "SuccessVIP":
                    return function(){
                        $location.path('/');
                    };
                    break;

                case "":
                    return function () {
                        if (window.WebViewJavascriptBridge) {
                            WebViewJavascriptBridge.callHandler('exitUIWebView');
                        } else if (browser.versions.android) {
                            try {
                                jsb.require('exitUIWebView', { "msg": "navigate back" }, function (response) {
                                    //Log.log(response);
                                });
                            }
                            catch (e) {
                                console.log(e)
                            }
                        }
                    };
                    break;

                case "search":
                    return function(){
                        if (window.WebViewJavascriptBridge) {
                            WebViewJavascriptBridge.callHandler('exitUIWebView');
                        } else if (browser.versions.android) {
                            try {
                                jsb.require('exitUIWebView', { "msg": "navigate back" }, function (response) {
                                    //Log.log(response);
                                });
                            }
                            catch (e) {
                                console.log(e)
                            }
                        }
                    };
                    break;

                case "friends":
                        return function(){
                            if (window.WebViewJavascriptBridge) {
                                WebViewJavascriptBridge.callHandler('exitUIWebView');
                            } else if (browser.versions.android) {
                                try {
                                    jsb.require('exitUIWebView', { "msg": "navigate back" }, function (response) {
                                        //Log.log(response);
                                    });
                                }
                                catch (e) {
                                    console.log(e)
                                }
                            }
                        };
                    break;

                case "consultant":
                    if(subPosition == 'index'){
                        return function () {
                            if (window.WebViewJavascriptBridge) {
                                WebViewJavascriptBridge.callHandler('exitUIWebView');
                            } else if (browser.versions.android) {
                                try {
                                    jsb.require('exitUIWebView', { "msg": "navigate back" }, function (response) {
                                        //Log.log(response);
                                    });
                                }
                                catch (e) {
                                    console.log(e)
                                }
                            }
                        };
                    }
                    break;
            }

        };
        return {
            back: function(){
                var returnUrl=$location.search().backUrl;
                //console.log("++++++returnUrl++++++" + returnUrl);
                if($rootScope.registerBack){
                    $rootScope.registerBack = false;
                    $location.url('/');
                    return;
                }
                if(returnUrl){
                    $location.url(returnUrl);
                    return;
                }
                var urlKey = $location.path().split("/")[1] || '';
                var urlSubKey = $location.path().split("/")[2] || '';

                (getBackUrl())();
            }
        }
});

app.factory('focus', function ($rootScope, $timeout) {
    return function (name) {
        $timeout(function () {
            $rootScope.$broadcast('focusOn', name);
        });
    }
});

// filter capital letters
app.filter('capitalLetter', function () {
    return function (str) {
        var tmpStr = str.toLowerCase();
        var len = tmpStr.length;
        if (tmpStr && len) {
            var tmpChar = tmpStr.substring(0, 1).toUpperCase();
            var otherChars = tmpStr.substring(1, len);
            tmpStr = tmpChar + otherChars;
        }
        return tmpStr;
    }
});

// localization module
angular.module('localization', []).factory('localize', ['$http', '$rootScope', '$window', '$filter', function ($http, $rootScope, $window, $filter) {
    var localize = {
        language: $window.navigator.userLanguage || $window.navigator.language,
        dictionary: {},
        resourceFileLoaded: false,

        successCallback: function (data) {
            localize.dictionary = data;
            localize.resourceFileLoaded = true;
            $rootScope.$broadcast('localizeResourcesUpdates');
        },

        initLocalizedResources: function () {
            var url = 'i18n/resources-locale_' + localize.language + '.js';
            $http({method: "GET", url: url, cache: false}).success(localize.successCallback).error(function () {
                // default language en-US
                var url = 'i18n/resources-locale_en-US.js';
                $http({method: "GET", url: url, cache: false}).success(localize.successCallback);

            });
        },

        getLocalizedString: function (value) {
            var result = '';
            if (!localize.resourceFileLoaded) {
                localize.initLocalizedResources();
                localize.resourceFileLoaded = true;
                return result;
            }
            if (localize.dictionary) {
                var entry = localize.dictionary[value];
                if ((entry !== null) && (entry != undefined)) {
                    result = entry;
                }
            }
            return result;
        }
    };
    return localize;
}]).
    filter('i18n', ['localize', function (localize) {
        return function (input) {
            return localize.getLocalizedString(input);
        };
    }]);